import {COMMON_PARAMS, METHOD, CLIENT} from './config.js'
import {sendRequest, mobileUrl, imageUrl} from './base.js'

// 订单:创建订单 POST
const ORDER_CREATE_ORDER = 'order/createOrder.do'

// 订单:订单列表分页
export const ORDER_LIST_PAGE = 'order/queryByPage.do'

// 订单:获取订单列表header数量显示
const ORDER_LIST_COUNT = 'order/amount.do'

// 订单:修改订单状态 GET
const ORDER_UPDATE_STATUS = 'order/update/command.do'

// 取消订单 GET
const ORDER_CANCEL = 'order/cancelOrder/'

// 查询订单物流信息
const ORDER_EXPRESS = 'express/queryExpress/'

/**
 * 创建订单
 * @param params
 * {
 *  cars:'xxx1,xxx2',
 *  invoiceTitle:"发票抬头",
 *  mark:"订单备注",
 *  productCategory:"商品类型",
 *  userDbId:userDbId,
 *  addressDbId:addressDbId
 * }
 * @returns {*}
 */
export function createOrder(params) {
  let data = Object.assign(params || {}, COMMON_PARAMS, {client: CLIENT})
  return sendRequest(imageUrl(ORDER_CREATE_ORDER), data, METHOD.POST)
}

/**
 * 分页查询订单列表
 * @param params
 * params: {pageSize:10,pageNum:0,sort:'createdDt',order:'desc',status:1}
 * @returns {*}
 * TODO:奇葩pageNum参数是从0开始的
 */
export function queryByPage(params) {
  let data = Object.assign(params || {}, COMMON_PARAMS)
  return sendRequest(imageUrl(ORDER_LIST_PAGE), data)
}

/**
 * 查询某个类型的订单数量,用于列表表头角标显示
 * @param params
 * params: {status:1}
 * @returns {*}
 * TODO:这里也非常奇葩,首先接口返回内容，就是一个数字，毫无规范可言；其次三种状态请求三次，还需要用户主动触发
 */
export function listCount(params) {
  let data = Object.assign(params || {}, COMMON_PARAMS)
  return sendRequest(imageUrl(ORDER_LIST_COUNT), data)
}


/**
 * 修改订单状态
 * @deprecated
 * @param params
 * {
 *  dbId: xxx,
 *  status: -1 // 可选值:{-1:'取消订单',-2:'删除订单'}
 * }
 * @returns {*}
 */
export function updateOrderStatus(params) {
  let data = Object.assign(params || {}, COMMON_PARAMS)
  return sendRequest(imageUrl(ORDER_UPDATE_STATUS), data)
}

/**
 * 修改订单状态
 * @param orderId
 * @returns {*}
 */
export function cancelOrder(orderId) {
  return sendRequest(mobileUrl(ORDER_CANCEL + orderId))
}

/**
 * 查询订单物流信息
 * @param orderId
 * @returns {*}
 */
export function expressOrder(orderId) {
  return sendRequest(mobileUrl(ORDER_EXPRESS + orderId))
}
